<template>
  <div class="container">
    <div class="add col-xs-12 col-sm-6 col-md-4 col-lg-3">
      <div class="card card-new" :class="{'active':activeCardIndex===0}" @mouseenter="changeCard($event,0)">
        <div class="flip" :class="{'in':activeCardIndex!==0||!showEditModal,'out':activeCardIndex===0&&showEditModal}">
          <i class="fa fa-plus" @click="openEditForm(null,1)"></i>
          <p class="text-new" @click="openEditForm(null,1)">新建设备</p>
        </div>
        <div class="flip flip-edit" :class="{'out':activeCardIndex!==0||!showEditModal,'in':activeCardIndex===0&&showEditModal}">
          <div v-show="showEditModal">新建成功</div>
        </div>
      </div>
    </div>
    <div class="col-xs-12 col-sm-6 col-md-4 col-lg-3" v-for="(item,index) in data">
      <div class="card" :class="{'active':activeCardIndex===index+1}" @mouseenter="changeCard($event,index+1)" @mouseleave="showDeleteModal=false">
        <div class="flip" :class="{'in':activeCardIndex!==index+1||(!showEditModal&&!showDeleteModal),'out':activeCardIndex===index+1&&(showEditModal||showDeleteModal)}">
          <h5 class="text-caption">{{item.name}}</h5>
          <ul>
            <li v-for="(val,key) in item" v-if="key!=='name'&&key!=='id'">
              <span class="text-title">{{key}}：</span>
              <span class="text-info">{{val?val:'无'}}</span>
            </li>
          </ul>
          <span class="button-group">
            <i class="fa fa-edit" @click="openEditForm(item.id,2)" title="修改"></i>
            <i class="fa fa-close" title="删除" @click="showDeleteModal=true"></i>
          </span>
        </div>
        <div class="flip flip-edit" :class="{'out':activeCardIndex!==index+1||!showEditModal,'in':activeCardIndex===index+1&&showEditModal}">
          <div v-show="showEditModal">修改成功</div>
        </div>
        <div class="flip flip-delete" :class="{'out':activeCardIndex!==index+1||!showDeleteModal,'in':activeCardIndex===index+1&&showDeleteModal}">
          <div v-show="showDeleteModal">
            <p>确定删除？</p>
            <div>
              <span @click="deleteData(item.id)">确定</span>
              <span @click="showDeleteModal=false">取消</span>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="edit-form" v-if="editingObj">
      <form method="post" :class="{'active':finalPosition}" :style="initialPosition" ref="form" id="form">
        <input type="hidden" name="cfgDevice.deviceID" v-model="editingObj.deviceID">
        <input type="hidden" name="cfgDevice.stationID" v-model="stationID">
        <div class="form-item">
          <label><span>局房：</span>
            <select data-placeholder="请选择局房" class="form-control" name="cfgDevice.houseID" v-model="editingObj.houseID">
              <option v-for="item in house" :value="item.houseID">{{item.houseName}}</option>
            </select>
          </label>
        </div>
        <div class="form-item">
          <label><span>设备类型：</span>
            <select data-placeholder="请选择设备类型" class="form-control" name="cfgDevice.devTypeID" v-model="editingObj.devTypeID">
              <option v-for="item in deviceType" :value="item.typeID">{{item.typeName}}</option>
            </select>
          </label>
        </div>
        <div class="form-item">
          <label><span>所属工作站：</span>
            <select data-placeholder="请选择工作站" class="form-control" name="cfgDevice.workStationID" v-model="editingObj.workStationID">
              <option v-for="item in workStation" :value="item.workStationID">{{item.workStationName}}</option>
            </select>
          </label>
        </div>
        <div class="form-item">
          <label><span>通信端口：</span>
            <select data-placeholder="请选择通信端口" class="form-control" name="cfgDevice.portID" v-model="editingObj.portID">
              <option v-for="item in port" :value="item.portID">{{item.commParam}}</option>
            </select>
          </label>
        </div>
        <div class="form-item">
          <label><span>设备名称：</span>
            <input type="text" placeholder="请输入设备名称" class="form-control" name="cfgDevice.deviceName" v-model="editingObj.deviceName">
          </label>
        </div>
        <div class="form-item">
          <label><span>拨码地址：</span>
            <input type="text" placeholder="请输入拨码地址" class="form-control" name="cfgDevice.address" v-model="editingObj.address">
          </label>
        </div>
        <div>
          <button type="button" class="btn btn-success" @click="submitData">保存</button>
          <button type="button" class="btn btn-white" @click="closeEditForm(false)">取消</button>
        </div>
      </form>
    </div>
  </div>
</template>

<script>
  export default {
    data () {
      return {
        stationID : sessionStorage.getItem("the_station_id"),
        workStation: [],
        house: [],
        deviceType: [],
        port: [],
        data: [],
        editingObj: null,
        type: null,
        activeCardIndex: -1,
        top: null,
        left: null,
        width: null,
        finalPosition: false,
        showEditModal: false,
        showDeleteModal: false,
      }
    },
    computed: {
      initialPosition () {
        return {
          top: this.top,
          left: this.left,
          width: this.width,
          height: this.height
        }
      }
    },
    methods: {
      getData() {
        this.$ajax.get(`http://39.108.179.28:8080/powerManageApi/api/BaseCfgDevice/list?curPage=1&stID=${this.stationID}`)
          .then(res => {
            if (res.data.code == 1) {
              let result = []
              res.data.result.devices && res.data.result.devices.reverse().forEach( (item, index) => {
                let obj = {}
                obj['id'] = item.deviceID
                obj['name'] = item.deviceName
                obj['所属局房'] = item.houseName
                obj['所属工作站'] = item.workStationName
                obj['设备名称'] = item.deviceName
                obj['设备类型'] = item.typeName
                result[index] = obj
              })
              this.data = result
            }
          })
      },
      getWorkStationData(baseStationID) {
        this.$ajax.get(`http://39.108.179.28:8080/powerManageApi/api/CommonAPI/getWorkStationByStID?stID=${baseStationID}`)
          .then(res => {
            if (res.data.code == 1) {
              this.workStation = res.data.result.workStations
            }
          })
      },
      getHouseData(baseStationID) {
        this.$ajax.get(`http://39.108.179.28:8080/powerManageApi/api/CommonAPI/getHouseByStID?stID=${baseStationID}`)
          .then(res => {
            if (res.data.code == 1) {
              this.house = res.data.result.houseList
            }
          })
      },
      getDeviceTypeData() {
        this.$ajax.get(`http://39.108.179.28:8080/powerManageApi/api/BaseStdDevType/list`)
          .then(res => {
            if (res.data.code == 1) {
              this.deviceType = res.data.result.stdDevTypes
            }
          })
      },
      getPortData(baseStationID) {
        this.$ajax.get(`http://39.108.179.28:8080/powerManageApi/api/CommonAPI/getPortByStID?stID=${baseStationID}`)
          .then(res => {
            if (res.data.code == 1) {
              this.port = res.data.result.ports
            }
          })
      },
      changeCard($event,index) {
        this.top = $event.srcElement.getBoundingClientRect().top + 'px'
        this.left = $event.srcElement.getBoundingClientRect().left + 'px'
        this.width = $event.srcElement.getBoundingClientRect().width + 'px'
        this.height = $event.srcElement.getBoundingClientRect().height + 'px'
        this.activeCardIndex = index
        this.showEditModal = false
      },
      submitData() {
        $.ajax({
          type: 'POST',
          url: `http://39.108.179.28:8080/powerManageApi/api/BaseCfgDevice/save?type=${this.type}`,
          data: $("#form").serialize(),
          success: (data) => {
            if(data.code === 1) {
              setTimeout(() => {
                this.getData()
              },500)
              this.closeEditForm(true)
            }
          }
        });
      },
      deleteData(id) {
        $.ajax({
          type: 'POST',
          url: `http://39.108.179.28:8080/powerManageApi/api/BaseCfgDevice/del?deviceID=${id}`,
          success: (data) => {
            if(data.code === 1) {
              this.getData()
              this.showDeleteModal = false
            }
          }
        });
      },
      openEditForm(id,type) {
        this.type = type
        if(!id){
          this.editingObj = {}
          setTimeout(()=>{
            this.finalPosition=true
          },100)
        }
        else {
          this.$ajax.get(`http://39.108.179.28:8080/powerManageApi/api/BaseCfgDevice/info?deviceID=${id}`)
            .then(res => {
              if (res.data.code == 1) {
                this.editingObj = res.data.result.cfgDevice
              }
              setTimeout(()=>{
                this.finalPosition=true
              },100)
            })
        }
      },
      closeEditForm(showEditModal) {
        setTimeout(()=>{
          this.editingObj = null
          if(showEditModal) {
            this.showEditModal = true
          }
        },500)
        this.finalPosition = false
      }
    },
    mounted () {
      this.getData()
      this.getHouseData(this.stationID)
      this.getDeviceTypeData()
      this.getWorkStationData(this.stationID)
      this.getPortData(this.stationID)
    }
  }
</script>

<style scoped>
  .card {
    position: relative;
    background-color: #ffffff;
    min-height: 240px;
    margin: 20px 0;
    border-radius: 5px;
    box-shadow: 0 0 5px 4px rgba(7, 17, 27, 0.1);
    box-sizing: border-box;
    border: 2px solid transparent;
  }
  @keyframes ants {
    to { background-position: 100% }
  }
  .card.active {
    background: linear-gradient(white, white) padding-box,repeating-linear-gradient(-45deg,rgb(3,153,127) 0, rgb(3,153,127) 25%, white 0, white 50%) 0 / .6em .6em;
    animation: ants 12s linear infinite;
  }
  .card .text-caption {
    font-size: 16px;
    font-weight: bold;
    padding: 0 10px 10px;
  }
  .card .text-new {
    font-size: 20px;
    margin: 10px 0 0;
    cursor: pointer;
  }
  .card li {
    display: flex;
    margin: 0 10px 10px;
    text-align: left;
  }
  .card .text-title {
    width: 95px;
    text-align: right;
  }
  .card .text-info {
    flex: 1;
  }
  @keyframes flipout {
    from {
      transform: rotateY(0);
    }
    to {
      transform: rotateY(-90deg);
    }
  }
  @keyframes flipin {
    from {
      transform: rotateY(90deg);
    }
    to {
      transform: rotateY(0);
    }
  }
  .flip {
    display: flex;
    flex-direction: column;
    align-items: left;
    justify-content: center;
    position: absolute;
    width: 100%;
    height: 100%;
    padding: 10px;
    transform: translateX(0);
    transform-origin: left;
    backface-visibility: hidden;
  }
  .card-new .flip,.flip-edit,.flip-delete {
    align-items: center;
  }
  .flip-edit,.flip-delete {
    padding: 0;
    font-size: 30px;
    color: #ffffff;
    font-family: 'cyjianxke5e69af301afc9';
    background: linear-gradient(45deg,rgba(3,153,127,1), rgba(3,153,127,.1));
  }
  .flip-delete {
    transform-origin: right;
    background: linear-gradient(-45deg, rgba(228, 57, 60,1), rgba(228, 57, 60,.1));
  }
  .flip-delete span {
    font-size: 20px;
    padding: 10px;
    cursor: pointer;
  }
  .flip-delete span:hover {
    text-shadow: -2px -2px rgba(0, 0, 0, .7);
  }
  .flip.in {
    animation: flipin .5s;
  }
  .flip.out {
    transform: rotateY(-90deg);
    animation: flipout .5s;
  }
  .fa-plus {
    padding: 5px 8px;
    font-size: 30px;
    color: rgb(3,153,127);
    border-radius: 50%;
    box-shadow: 0 0 5px 5px rgba(3,153,127,.5);
    cursor: pointer;
  }
  .button-group {
    position: absolute;
    top: 4%;
    right: 4%;
    cursor: pointer;
  }
  .button-group>i {
    font-size: 16px;
    padding: 0 3px;
  }
  .button-group>i:hover {
    color: #42b983;
  }
  .edit-form {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: 1000;
    background: rgba(121, 128, 121,.6);
  }
  .edit-form> form {
    position: absolute;
    background: #ffffff;
    padding: 10px;
    border-radius: 5px;
    overflow: hidden;
    transition: all .5s linear;
  }
  .edit-form> form.active {
    transition: all .5s linear;
    top: calc(50vh - 164px)!important;
    left: calc(50vw - 159px)!important;
    width: 318px!important;
    height: 328px!important;
    max-height: 100%;
    overflow: auto;
  }
  .form-item>label {
    display: flex;
    margin: 10px 5px;
  }
  .form-item>label>span {
    width: 90px;
    text-align: right;
    line-height: 28px;
  }
  .form-item>label>*:last-child {
    flex: 1;
  }
  @media screen and (max-width: 767px){
    ::-webkit-scrollbar{
      width:0;height:0
    }
    .card .text-new {
      font-size: 16px;
    }
    .edit-form> form {
      width: 100%;
      left: 0;
    }
    .edit-form> form.active {
      width: 100%!important;
      left: 0!important;
    }
  }
</style>
